<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>{{materielOption.name}}-入库记录</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
<!--        <div class="hearder-search" >-->
<!--          <SearchModle dataKey="bookCode" @searchFn="searchFn"></SearchModle>-->
<!--          <span class="allbook">总 {{page.total}} 条</span>-->
<!--        </div>-->
        <!--本体列表，数据 -->
        <el-table
            ref="multipleTable"
            :data="arryList"
            :stripe="tableStripe"
            :border="tableBorder"
            tooltip-effect="dark"
            :height="tableHeight"
            header-cell-class-name="table-text-c"
            cell-class-name="table-text-c">
          <el-table-column
              align="center"
              prop="id"
              label="序号"
              width="120">
          </el-table-column>
          <el-table-column
              align="center"
              label="物料名称">
            <template slot-scope="scope">
              {{scope.row.materiel.name}}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="create_time"
              label="入库时间"
              width="180">
          </el-table-column>
          <el-table-column
              align="center"
              label="入库数量"
              prop="number"
              width="100">
          </el-table-column>
          <el-table-column
              align="center"
              label="入库人"
              width="150">
            <template slot-scope="scope">
              {{scope.row.examine.realname}}
            </template>
          </el-table-column>
        </el-table>
        <!--工具条 分页-->
        <el-col :span="24" class="toolbar">
          <el-pagination
              background
              :page-size="page.pagesize"
              :current-page="page.pageindex"
              layout="total, prev, pager, next"
              :total="page.total"
              @current-change="handleSizeChange"></el-pagination>
        </el-col>
        <el-button @click="exportData" size="small" type="primary" style="margin-bottom: 10px;">导出</el-button>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small" >确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {pagination, yearDay} from '@/utils/util.js'

export default {
  props: ['visible','materielOption'], // commoent 学生点评 collection 学生采集
  components: {
  },
  data() {
    return {
      dialogVisible: false,
      yeartimeFn: yearDay,
      tableStripe: true, // 是否斑马栏
      tableBorder: true, // 单元格右边边框
      arryList: [], // 数据列表
      // 页数
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      searchForm: {},
      tableHeight: 0
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if(this.visible){
        let n =this;
        this.$nextTick(()=>{
          let height= n.$refs.dialogBox.offsetHeight;  //top 50 1 search 50 page 50 10 footer 50 1
          this.tableHeight = height - (51+40+60+51)
          console.log('height:', height)
        })
        this.InitFn()
      }else{
        this.arryList = [];
      }
    }
  },
  created() {
  },
  methods: {
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option){
      return JSON.parse(JSON.stringify(option))
    },
    // 初始化
    async InitFn () {
      this.searchForm.p = this.page.pageindex;
      this.searchForm.materiel_id = this.materielOption.id;
      this.searchForm.type = 0;
      this.searchForm.status = 1;
      let data = await this.$fetch(this.api.admin_materiel_log_index, this.searchForm);
      if (!data.code) {
        this.arryList = data.list.data
        this.page.total = data.list.total
      } else {
      }
    },
    // 搜索组件
    searchFn(option) {
      this.page.pageindex = 1
      this.searchForm = option
      this.InitFn()
    },
    // 分页
    handleSizeChange (val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 导出数据
    async exportData() {
      const params = {
        materiel_id: this.materielOption.id,
        type: 0,
      };
      const response = await this.$http({
        method: 'get',
        url: '/admin/'+this.api.materiel_export,
        params: params,
        responseType: 'arraybuffer'
      });
      if (response.status === 200) {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', this.materielOption.name+'入库记录.xlsx');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      } else {
        alert('网络请求出错');
      }
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 80%;
  height: 75%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding:20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.toolbarLine {
  text-align: center;
}
.hearder-search{
  display: flex;
  align-items: center;
}
</style>


